import Vue from 'vue'
import Router from 'vue-router'
import Main from '@/view/Main.vue'
import {onMenuShareAppMessage, onMenuShareTimeline} from '@/assets/js/common.js'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/index',
      component: resolve => require(['@/view/Main.vue'], resolve),
      children:[       
        {
          path: '/index',
          name: 'index',
          title: '首页',
          component: resolve => require(['@/view/index'], resolve)
        },
        {
          path: '/createArticle',
          component: resolve => require(['@/view/createArticle'], resolve)
        },
        {
          path: '/onlineTools',
          component: resolve => require(['@/view/onlineTools'], resolve)
        },
        {
          path: '/chat',
          component: resolve => require(['@/view/chat'], resolve)
        },
        {
          path: '/onlineTools/coderunner',
          component: resolve => require(['@/view/onlineTools/coderunner'], resolve)
        },
        {
          path: '/onlineTools/imageEdit',
          component: resolve => require(['@/view/onlineTools/imageEdit'], resolve)
        },
        {
          path: '/onlineTools/getImage',
          component: resolve => require(['@/view/onlineTools/getImage'], resolve)
        },
        {
          path: '/articledetails/:id',
          component: resolve => require(['@/view/articledetails'], resolve)
        },
        {
          path: '/newsdetails/:id',
          component: resolve => require(['@/view/newsdetails'], resolve)
        },
      ]
    },
  ]
})

// 路由每次跳转都重置分享
router.beforeEach(function (to, from, next) {
  // console.log(Vue.wechat)
  Vue.wechat.ready(function () {
    let link = window.location.href.split('#')[0] + '#/share?t=0'
    let imgUrl = 'https://dushiren.oss-cn-shenzhen.aliyuncs.com/images/magazine/0618a456389f1d8f03f05b754e437352.jpg'
    // 分享给朋友
    let personTitle = '市少先队队刊手机订阅平台'
    let desc = `青少年精品课外读物，发现语文之美。陪伴孩子成长，和孩子一起爱上阅读`
    onMenuShareAppMessage(Vue.wechat, personTitle, desc, link, imgUrl)
    // 分享到朋友圈
    let circleTitle = '市少先队队刊手机订阅平台'
    onMenuShareTimeline(Vue.wechat, circleTitle, link, imgUrl)
  })
  next()
})

// 路由每次跳转都回到顶部
router.afterEach(function (to, from, next) {
  setTimeout(() => {
    window.scrollTo(0, 0)
  }, 0)
})

export default router
